package com.oocl.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class DBUtilTest {
	private static String className = "oracle.jdbc.OracleDriver";
	private static final String url = "jdbc:oracle:thin:@10.222.29.165:1521:xe";
	private static final String username = "group4";
	private static final String pwd = "123456";
	
	public static Connection createConnection(){
		Connection con = null;
		try {
			Class.forName(className);
			con = DriverManager.getConnection(url, username, pwd);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
	
	/**
	 * 老师的BasicDataSource 是个单例模式，只加载一次
	 */
	
	/**
	 * @param con
	 * @param pst
	 * @param rs
	 */
	public static void close(Connection con, PreparedStatement pst, ResultSet rs){
		try {
			if(rs != null)
				rs.close();
			if(pst != null)
				pst.close();
			if(con != null)
				con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * 
	 * @param count
	 * @return
	 */
	public static DataSource createConnectionWithDataSource(int maxActive){
		/**
		 * 向我这样写就太占内存了，整个项目只需要一个DataSource即可
		 */
		BasicDataSource s = new BasicDataSource();
		s.setUrl(url);
		s.setDriverClassName(className);
		s.setPassword(pwd);
		s.setUsername(username);
		s.setMaxActive(maxActive);
		return s;
	}

}
